package com.dearbinge.data.service.impl.moments;


import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.moments.BusinessCircleCommentService;
import com.dearbinge.data.api.moments.BusinessCircleService;
import com.dearbinge.data.dao.ShopManageMapper;
import com.dearbinge.data.dao.ThumbsMapper;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.collect.CommodityCollectMapper;
import com.dearbinge.data.dao.moments.BusinessCircleClickMapper;
import com.dearbinge.data.dao.moments.BusinessCircleMapper;
import com.dearbinge.data.pojo.collect.Collect;
import com.dearbinge.data.pojo.moments.BusinessCircle;
import com.dearbinge.data.pojo.moments.BusinessCircleClickDO;
import com.dearbinge.data.pojo.moments.vo.BusinessCircleClickResponseVO;
import com.dearbinge.data.pojo.moments.vo.BusinessCircleCommentVO;
import com.dearbinge.data.pojo.moments.vo.BusinessCircleVO;
import com.dearbinge.data.pojo.shopManage.ShopManage;
import com.dearbinge.data.pojo.thumbs.Thumbs;
import com.dearbinge.data.pojo.thumbs.vo.ThumbsResult;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.utils.PageUtils;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import com.dearbinge.utils.enums.TypeCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 社交圈业务层
 *
 * @author wes
 * @date 2019/1/3
 */
@Service(value = "businessMessageService")
public class BusinessCircleServiceImpl implements BusinessCircleService {

    @Autowired
    private BusinessCircleMapper businessCircleMapper;
    @Autowired
    private ThumbsMapper thumbsMapper;
    @Autowired
    private CommodityCollectMapper commodityCollectMapper;
    @Autowired
    private ShopManageMapper shopManageMapper;
    @Autowired
    private BusinessCircleCommentService businessCircleCommentService;
    @Autowired
    private BusinessCircleClickMapper businessCircleClickMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 增加点击量
     *
     * @param clickId 表id
     * @param type    1：商品 2：客服 3：分享 4：秀场
     * @return 影响行数
     */
    @Override
    public Integer updateClick(Integer clickId, Integer type) {
        return businessCircleClickMapper.updateClick(clickId, type);
    }

    /**
     * 社交圈动态 删除
     *
     * @param circleId 主键id
     * @return 结果
     */
    @Override
    public Integer delete(Integer circleId) {

        return businessCircleMapper.delete(circleId);
    }


    /**
     * 社交圈动态
     *
     * @param id 表id
     * @param userId  当前登录用户id
     * @param current 当前页
     * @param size    每页展示条数
     * @param type  0 全部 1 商品 2生活
     * @return 分页结果
     */
    @Override
    public Page listBusinessCircle(String userId, Integer id, Integer current, Integer size, Integer type) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page page = new Page(current, size);
        Integer total = 0;
        List<BusinessCircleVO> list =null;
        BusinessCircleClickResponseVO businessCircleClickResponseVO = new BusinessCircleClickResponseVO();
        switch (type) {
            case 0:
                list = businessCircleMapper.listBusinessCircle(userId, id, 0, page);
                for (BusinessCircleVO businessCircleVO : list) {
                    //商品信息
                    if (businessCircleVO.getTableType().equals(TypeCodeEnum.BUSINESS_CIRCLE_COMMODITY.getCode())) {
                        commodityParams(userId, businessCircleVO,businessCircleClickResponseVO);
                        setThumbsAndComment(userId, businessCircleVO, thumbsCommodityCode, commentCommodityCode,businessCircleClickResponseVO);
                    }
                    //生活信息
                    if (businessCircleVO.getTableType().equals(TypeCodeEnum.BUSINESS_CIRCLE_LIFE.getCode())) {
                        setThumbsAndComment(userId, businessCircleVO, thumbsLifeCode, commentLifeCode,businessCircleClickResponseVO);
                    }
                }
                total = businessCircleMapper.countMessageByType(userId,id, 0);
                break;
            case 1:
                list = businessCircleMapper.listBusinessCircle(userId, id, 1, page);
                for (BusinessCircleVO businessCircleVO : list) {
                    commodityParams(userId,businessCircleVO,businessCircleClickResponseVO);
                    setThumbsAndComment(userId, businessCircleVO,thumbsCommodityCode, commentCommodityCode,businessCircleClickResponseVO);
                }
                total = businessCircleMapper.countMessageByType(userId,id, 1);
                break;
            case 2:
                list = businessCircleMapper.listBusinessCircle(userId, id, 2, page);
                for (BusinessCircleVO businessCircleVO : list) {
                    setThumbsAndComment(userId, businessCircleVO,thumbsLifeCode, commentLifeCode,businessCircleClickResponseVO);
                }
                total = businessCircleMapper.countMessageByType(userId, id,2);
                break;
            default:
                break;
        }
        page.setRecords(list);
        page.setTotal(total);
        return page;
    }





    private void setUserParams(BusinessCircleVO businessCircleVO, int id) {
         //插入用户头像和名字
        User user = userMapper.getById(id);
        businessCircleVO.setUserName(user.getName());
        businessCircleVO.setHeadImg(user.getHeadImg());
    }


    private void commodityParams(String loginUser, BusinessCircleVO businessCircleVO, BusinessCircleClickResponseVO businessCircleClickResponseVO) {
        //商品收藏
        Collect collect = commodityCollectMapper.getCollect(loginUser,businessCircleVO.getTableId().toString());
        if (collect != null) {
            businessCircleVO.setCollect(true);
        }
        //店铺id
        ShopManage shopManage = shopManageMapper.getByUserId(businessCircleVO.getUserId());
        if (shopManage != null) {
            businessCircleVO.setCommodityShopId(shopManage.getId().toString());
        }
        //点击量
        Integer tableId = businessCircleVO.getTableId();
        BusinessCircleClickDO circleClickDO = businessCircleClickMapper.getByCommodityId(tableId);
        if (circleClickDO != null) {
            BeanUtils.copyProperties(circleClickDO, businessCircleClickResponseVO);
            businessCircleVO.setClickList(businessCircleClickResponseVO);
        }

    }

    private void setThumbsAndComment( String userId,BusinessCircleVO businessCircleVO,  Integer thumbsType,
                                      Integer commentType, BusinessCircleClickResponseVO businessCircleClickResponseVO) {
        //插入 自己是否点赞 点赞集合 评论集合
        String id = businessCircleVO.getTableId().toString();
        Thumbs thumbs = thumbsMapper.getThumbs( userId, thumbsType, id);
        List<ThumbsResult> thumbsResultList = thumbsMapper.listUserThumbs(id, thumbsType);
        List<BusinessCircleCommentVO> commentReplyResults = businessCircleCommentService.listCommentById(commentType, businessCircleVO.getTableId());
        if (thumbs != null) {
            businessCircleVO.setThumbs(true);
        }
        businessCircleVO.setCommentReplyResultList(commentReplyResults);
        businessCircleVO.setThumbsResultList(thumbsResultList);
        businessCircleClickResponseVO.setCommentTotal(commentReplyResults.size());
        businessCircleClickResponseVO.setThumbsTotal(thumbsResultList.size());
    }


    private int thumbsLifeCode = TypeCodeEnum.THUMBS_LIFE.getCode();

    private int commentLifeCode = TypeCodeEnum.COMMENT_LIFE.getCode();

    private int thumbsCommodityCode = TypeCodeEnum.THUMBS_COMMODITY.getCode();

    private int commentCommodityCode = TypeCodeEnum.COMMENT_COMMODITY.getCode();

    /*————————————————————————废弃的接口 新版本上线后删除————————————————————————————————————*/




    /**
     * 查询个人全部社交圈
     *
     * @param userId  用户id
     * @param current 当前页
     * @param size    每页展示条数
     * @param type    0:全部 1：商品 2：生活
     * @return 分页结果
     */
    @Override
    public Page listAllMomentsByUserId(String userId, Integer current, Integer size,Integer type) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        //分页
        Page page = new Page(current, size);
        List<BusinessCircle> businessCircles = null;
        Long count = null;
        switch(type){
            case 0:
                businessCircles = businessCircleMapper.listMyAllMomentsPage(userId,0, page);
                //查出总条数
                 count = businessCircleMapper.countMyAllMoments(userId,0);
                break;
            case 1:
                businessCircles = businessCircleMapper.listMyAllMomentsPage(userId,1, page);
                //查出总条数
                count = businessCircleMapper.countMyAllMoments(userId,1);
                break;
            case 2:
                businessCircles = businessCircleMapper.listMyAllMomentsPage(userId,2, page);
                //查出总条数
                count = businessCircleMapper.countMyAllMoments(userId,2);
                break;
            default:
                break;
        }




        //最大数
        page.setTotal(count);
        page.setRecords(businessCircles);
        return page;
    }

    /**
     * 查询单条动态详情
     *
     * @param userId    登录的用户id
     * @param id   表id
     * @param tableType 类型（0：商品  1：生活）
     * @return 动态详情
     */
    @Override
    public ResultMsg<BusinessCircleVO> getDynamicDetails(Integer userId, Integer id, Integer tableType) {
        BusinessCircleClickResponseVO businessCircleClickResponseVO = new BusinessCircleClickResponseVO();
        BusinessCircleVO businessCircleVO = new BusinessCircleVO();
        String userIdString = userId.toString();
        switch(tableType){
            case 0:
                //查询单条动态内容
                BusinessCircle commodityMessage = businessCircleMapper.getByTableId(id, tableType);
                if (commodityMessage == null) {
                    return  ResultMsg.error(ResultStatusCodeEnum.CODE5002.getCode(),"id不存在");
                }
                BeanUtils.copyProperties(commodityMessage,businessCircleVO);
                setUserParams(businessCircleVO, Integer.parseInt(commodityMessage.getUserId()));
                setThumbsAndComment(userIdString, businessCircleVO, thumbsCommodityCode, commentCommodityCode,businessCircleClickResponseVO);
                commodityParams(userIdString, businessCircleVO,businessCircleClickResponseVO);
                break;
            case 1:
                //查询单条动态内容
              BusinessCircle lifeMessage = businessCircleMapper.getByTableId(id, tableType);
                if (lifeMessage == null) {
                    return  ResultMsg.error(ResultStatusCodeEnum.CODE5002.getCode(),"id不存在");
                }
                BeanUtils.copyProperties(lifeMessage,businessCircleVO);
                setUserParams(businessCircleVO, Integer.parseInt(lifeMessage.getUserId()));
                setThumbsAndComment(userIdString, businessCircleVO,thumbsLifeCode, commentLifeCode,businessCircleClickResponseVO);
                commodityParams(userIdString,businessCircleVO,businessCircleClickResponseVO);
                break;
            default:
                break;
        }
        return ResultMsg.okData(businessCircleVO,"success");
    }


}
